//
//  QKUserAccount.m
//  QKWeibo-OC
//
//  Created by iMac_Kurt on 16/8/18.
//  Copyright © 2016年 Kurt_Qi. All rights reserved.
//

#import "QKUserAccount.h"
#import <MJExtension.h>

@implementation QKUserAccount
// MARK: - 计算过期日期
// 重写属性的set方法：expires_in一有值就计算expiresDate
- (void)setExpires_in:(NSTimeInterval)expires_in {
    _expires_in = expires_in;
    
    self.expiresDate = [NSDate dateWithTimeIntervalSinceNow:expires_in];
}


// MARK: - 重写了description描述信息
// 让控制台以字典形式打印模型数据
- (NSString *)description {
    NSArray *keys = @[@"access_token", @"expiresDate", @"uid", @"screen_name", @"avatar_large"];
    
    return [self dictionaryWithValuesForKeys:keys].description;
}

// MARK: - 储存用户账户数据（"键值"归档&解档）
// 存储用户账户数据
- (void)saveUserAccount {
    
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    path = [path stringByAppendingPathComponent:@"account.plist"];
    
    // 一定要检查是否真的储存了
    //NSLog(@"%@", path);
    
    [NSKeyedArchiver archiveRootObject:self toFile:path];
}
// 归档
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.access_token forKey:@"access_token"];
    [aCoder encodeObject:self.expiresDate forKey:@"expiresDate"];
    [aCoder encodeObject:self.uid forKey:@"uid"];
    [aCoder encodeObject:self.screen_name forKey:@"screen_name"];
    [aCoder encodeObject:self.avatar_large forKey:@"avatar_large"];
}
// 解档:根据键值使用解档器将对应的数据一一赋值给模型的属性
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self.access_token = [aDecoder decodeObjectForKey:@"access_token"];
    self.expiresDate = [aDecoder decodeObjectForKey:@"expiresDate"];
    self.uid = [aDecoder decodeObjectForKey:@"uid"];
    self.screen_name = [aDecoder decodeObjectForKey:@"screen_name"];
    self.avatar_large = [aDecoder decodeObjectForKey:@"avatar_large"];
    
    return self;
}

// MARK: - 字典转模型
+ (instancetype)initWithDict:(NSDictionary *)dict {
    return [self mj_objectWithKeyValues:dict];
}

@end
